package com.foreverwin.jzymes.production.inspect.controller;

import com.foreverwin.jzymes.common.domain.CarrierScanLog;
import com.foreverwin.jzymes.common.util.CarrierUtil;
import com.foreverwin.jzymes.common.util.CommonMethods;
import com.foreverwin.jzymes.framework.base.AjaxResult;
import com.foreverwin.jzymes.framework.exception.BusinessException;
import com.foreverwin.jzymes.framework.util.ObjectMapperUtil;
import com.foreverwin.jzymes.production.clamp.controller.ClampController;
import com.foreverwin.jzymes.production.inspect.service.IInspectAdviceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/production/inspect")
public class InspectController {

    private Logger logger = LoggerFactory.getLogger( ClampController.class );

    @Autowired
    IInspectAdviceService inspectAdviceService;

    @RequestMapping("/inspectCarrier")
    public AjaxResult inspectCarrier( @RequestBody String carrierScanLogJson ){
        logger.info( "接收到全检信息: " + carrierScanLogJson );
        try{
            CarrierScanLog carrierScanLog = ObjectMapperUtil.readValueForUnderscore( carrierScanLogJson, CarrierScanLog.class );
            carrierScanLog.setCarrier( CarrierUtil.buildCarrier( carrierScanLog.getSite(), carrierScanLog.getCarrier() ) );
            inspectAdviceService.inspectCarrier( carrierScanLog );
            logger.info( "全检信息处理完成: " + carrierScanLogJson );
            return AjaxResult.success();
        }catch ( Exception e ){
            logger.error( "全检信息处理失败: " + e.getMessage() + "，全检信息: " + carrierScanLogJson );
            return AjaxResult.error( e.getMessage() );
        }
    }

    @RequestMapping("/recordCarrierNC")
    public AjaxResult recordCarrierNC( @RequestBody Map<String,Object> jsonMap ) {
        jsonMap.put( "site", CommonMethods.getSite() );
        try{
            jsonMap.put( "carrier", CarrierUtil.buildCarrier( (String)jsonMap.get( "site" ), (String)jsonMap.get( "carrier" ) ) );
            inspectAdviceService.recordCarrierNC( jsonMap );
            return AjaxResult.returnSuccess();
        }catch ( BusinessException e ){
            return AjaxResult.returnError( e.getMessage() );
        }

    }

    @RequestMapping("/disposeCarrierNC")
    public AjaxResult disposeCarrierNC( @RequestBody Map<String,Object> jsonMap ) {
        jsonMap.put( "site", CommonMethods.getSite() );
        try{
            jsonMap.put( "carrier", CarrierUtil.buildCarrier( (String)jsonMap.get( "site" ), (String)jsonMap.get( "carrier" ) ) );
            inspectAdviceService.disposeCarrierNC( jsonMap );
            return AjaxResult.returnSuccess();
        }catch ( BusinessException e ){
            return AjaxResult.returnError( e.getMessage() );
        }
    }

}
